<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Common\ResponseCode;
use App\Models\AdminDeptMenuMapModel;
use App\Models\AdminDeptModel;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Validator;

class DeptController extends ApiController {
    /**
     * @param Request $request
     * 部门列表
     */
    public function deptList(Request $request)
    {
        $page = $request->input('page') ? $request->input('page') : 1;
        $pageSize = $request->input('pageSize') ? $request->input('pageSize') : 10;

        $deptList = AdminDeptModel::getRecordListCondition([], ['*'], [], $page, $pageSize);
        $deptCount = AdminDeptModel::getRecordCountCondition();

        return $this->apiResponse(['list' => $deptList, 'page' => $page, 'pageSize' => $pageSize, 'total' => $deptCount]);
    }

    /**
     * @param Request $request
     * @return void
     * 部门信息
     */
    public function deptInfo(Request $request)
    {
        $rule = [
            'deptId' => 'required|numeric',
        ];
        $messages = [
            'deptId.required' => '部门Id不能为空',
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, $error);
        }
        $deptId = $request->input('deptId');
        $deptInfo = AdminDeptModel::findRecordOneCondition(["id" => $deptId]);
        $deptMenuMap = AdminDeptMenuMapModel::getRecordListCondition(["deptId" => $deptId], ["menuId"])->toArray();
        $deptInfo["deptMenuMap"] = Arr::flatten($deptMenuMap);
        return $this->apiResponse($deptInfo);
    }

    /**
     * TODO:添加或者修改
     */
    public function addOrEdit(Request $request)
    {
        $rule = [
            'deptName' => 'required',
            'status' => 'required|numeric',
        ];
        $messages = [
            'deptName.required' => '部门名称不能为空',
            'status.required' => '状态不能为空',
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, $error);
        }
        $DeptName = $request->input('deptName');
        $Status = $request->input('status');
        $DeptId = $request->input('deptId');
        $menuIds = $request->input('menuIds'); // 菜单按钮逗号分隔的字符串
        $Sort = $request->input('sort');
        $data = [
            'deptName' => $DeptName,
            'status' => $Status,
            'sort' => $Sort,
        ];
        if ($DeptId == -1) {
            $data['CreateTime'] = date("Y-m-d H:i:s");
            $id = AdminDeptModel::addRecordGetId($data);
            if (!empty($menuIds)) {
                $deptMenuMap = [];
                foreach (explode(',', $menuIds) as $val) {
                    $deptMenuMap[] = [
                        "deptId" => $id,
                        "menuId" => $val
                    ];
                }
                AdminDeptMenuMapModel::addRecord($deptMenuMap);
            }
            return $this->apiResponse(null, ResponseCode::SUCCESS, '添加成功');
        } else {
            AdminDeptMenuMapModel::deleteRecordMass(["deptId" => $DeptId]);
            if (!empty($menuIds)) {
                $deptMenuMap = [];
                foreach (explode(',', $menuIds) as $val) {
                    $deptMenuMap[] = [
                        "deptId" => $DeptId,
                        "menuId" => $val
                    ];
                }
                AdminDeptMenuMapModel::addRecord($deptMenuMap);
            }
            AdminDeptModel::updateRecordDB($DeptId, $data);
            return $this->apiResponse(null, ResponseCode::SUCCESS, '修改成功');
        }
    }

    /**
     * @param Request $request
     * 删除
     */
    public function deptDel(Request $request)
    {
        $rule = [
            'deptId' => 'required',
        ];
        $messages = [
            'deptId.required' => '部门id不能为空'
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, $error);
        }

        $DeptId = $request->post('DeptId');
        $data = [
            'isDelete' => 1
        ];
        AdminDeptModel::updateRecordDB($DeptId, $data);
        return $this->apiResponse(null, ResponseCode::SUCCESS, '删除成功');
    }
}
